<?php

require_once("includes/sparqllib.php");
require_once("model/Dsb.class.php");
require_once("model/Observation.class.php");
require_once("model/Dimension.class.php");
require_once("model/DimensionValue.class.php");
require_once("model/Attribute.class.php");
require_once("model/QueryManager.class.php");

Class datasetController Extends baseController {

public function index() 
{
       
        $this->registry->template->show('dataset_index');
		
		
}

public function search() 
{
      
		$this->queryManager=QueryManager::GetInstance();
        $this->queryManager->iniConnection($this->parameters['endpoint']);
        $dimensions=$this->getAllDimensionsLabelsUnderScoreSeperatedNames($this->parameters['uri']);

		$this->registry->template->url="./index.php?uri=".str_replace('#','%23',$this->parameters['uri'])."&endpoint=".$this->parameters['endpoint']."&rt=dataset/exhibit";
		$this->registry->template->dimensions=$dimensions;
		
        $this->registry->template->show('dataset_exhibit');
		
		
}


public function view(){
   
        $this->queryManager=QueryManager::GetInstance();
       $this->queryManager->iniConnection($this->parameters['endpoint']);
      
        $dataset=$this->getdatasetMetadata($this->parameters['uri']);
      if(isset($dataset)){
			 echo '{"status":"ok","dataset":'.json_encode($dataset).'}';
        }
        else{ 
          echo '{"status":"error","msg":"No Data set information exists..."}';
 		 }    
       
        
}

public function exhibit(){
   
       $this->queryManager=QueryManager::GetInstance();
       $this->queryManager->iniConnection($this->parameters['endpoint']);
       $dataset=$this->getdatasetMetadataAndObservations($this->parameters['uri']);

      if(isset($dataset)){
			 echo $dataset;
        }
        else{ 
          echo '{"status":"error","msg":"No Data set information exists..."}';
 		 }    
       
        
}
public function createJsonExhibit($Observation){
$ob_str='{
    types: {
        "Observation" : {
            pluralLabel: "Observations"
        }
    },
    "items" : [';


$index=1;
foreach($Observation as $value)
{
		if($index==1){
			$ob_str.='{';
		}
		else{
		 $ob_str.=',{';
		}
		$ob_str.='type : "Observation" , ';
		$ob_str.='label : "Observation'.$index.'" , ';
		$ob_str.='uri : "'.$value->observationURI.'" , ';
		$ob_str.='url : "./index.php?uri='.str_replace('#','%23',$value->observationURI).'&endpoint='.$this->parameters["endpoint"].'" , ';
		$ob_str.='endpoint : "'.$this->parameters['endpoint'].'", ';
		$ob_str.='measure : "'.$value->measure->label.'" , ';
		$ob_str.='value : "'.$value->observationValue.'" , ';
		   for($i=0;$i<count($value->dimensions);$i++)
			{
				 $ob_str.='"'.str_replace(' ','_',$value->dimensions[$i]->label).'": "'.$value->dimensionValue[$i]->value.'" ';
				 if($i!=count($value->dimensions)-1){$ob_str.=' , ';}

			}
		$ob_str.='}';
		$index++;
 }

$ob_str.=' ]}';
return $ob_str;

}

public function getAllDimensionsLabelsUnderScoreSeperatedNames($dataset_uri)
{
	$this->queryManager=QueryManager::GetInstance();
	$sparql=$this->queryManager->getAllDimensionsLabelsUnderScoreSeperatedNamesQuery
($dataset_uri);

	$result = sparql_query( $sparql ); 
	if( !$result ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; }
	$dimensions=array();
	while ($row = sparql_fetch_array( $result )){
    $dimension=(isset($row['dimensionLabel'])) ? $row['dimensionLabel'] : "N/A";
    $dimensions[]=$dimension;
	}

return  $dimensions;
}

public function getdatasetMetadataAndObservations($dataset_uri){
		
		$this->queryManager=QueryManager::GetInstance();
		$sparql=$this->queryManager->getdatasetMetadataAndObservationsQuery($dataset_uri);
		$result = sparql_query( $sparql ); 
		if( !$result ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; }
		$i=0;
		$label='observation';
		$type= 'observation';
		$observations=array();
		$dim=Dimension::GetInstance();
		$dimval=DimensionValue::GetInstance();
		$isObservationRes=Observation::GetInstance();
		while ($row = sparql_fetch_array( $result )){
			

            $uri =$row['uri'];
			$dimension=(isset($row['dimensionLabel'])) ? $row['dimensionLabel'] : "N/A";
			$valueuri=(isset($row['valueuri'])) ? $row['valueuri'] : "N/A";
			$val=(isset($row['val'])) ? $row['val'] : "N/A";			
			$measureLabel=(isset($row['measureLabel'])) ? $row['measureLabel'] : "N/A";			
			$obsValue=(isset($row['obsValue'])) ? $row['obsValue'] : "N/A";

			if(!isset($observations['<'.$uri.'>'])){
              
			  $isObservationRes->measure->label=$measureLabel;
			  $isObservationRes->observationValue=$obsValue;
              $isObservationRes->observationURI=$uri;
             
			  $dim->label=$dimension;
			  $dimval->value=$val;
              $isObservationRes->dimensions[]=$dim->copy();
			  $isObservationRes->dimensionValue[]=$dimval->copy();

			  $observations['<'.$uri.'>']=$isObservationRes->copy();
			}
			else
			{
			  $isObservationRes=$observations['<'.$uri.'>'];
	 
			  $dim=Dimension::GetInstance();
			  $dimval=DimensionValue::GetInstance();
			  $dim->label=$dimension;
			  $dimval->value=$val;

              $isObservationRes->dimensions[]=$dim->copy();
			  $isObservationRes->dimensionValue[]=$dimval->copy();

			  $observations['<'.$uri.'>']=$isObservationRes->copy();
			}
			

	
		}

return $this->createJsonExhibit($observations);

}

public function getdatasetMetadata($dataset_uri){


		$this->queryManager=QueryManager::GetInstance();
		$sparql=$this->queryManager->getDataSetMetadataQuerydsURI($dataset_uri);
		$result = sparql_query( $sparql ); 
		
		if( !$result ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; }
		$row = sparql_fetch_array( $result );
		$dsobject=NULL;
		if(isset($row['label']) || isset($row['title'])){
		$ds=$dataset_uri;
		$label=(isset($row['label'])) ? $row['label'] : $row['title'];  
		$comment=(isset($row['comment'])) ? $row['comment'] : "N/A";  
		$dateds=(isset($row['date'])) ? $row['date'] : "N/A";  
		$subject=(isset($row['subject'])) ? $row['subject'] : "N/A";  
		$publisher=(isset($row['publisher'])) ? $row['publisher'] : "N/A";  
		$dsobject=Dataset::WrapDataset($ds,$label,$comment ,$dateds, $subject, $publisher);
		}	    
		 
 return $dsobject;

}

}
?>
